package org.dromara.bclt.service.impl;

import org.dromara.bclt.domain.DeviceInfo;
import org.dromara.bclt.domain.vo.DeviceInfoVo;
import org.dromara.bclt.mapper.DeviceInfoMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bclt.domain.bo.DataRealtimeBo;
import org.dromara.bclt.domain.vo.DataRealtimeVo;
import org.dromara.bclt.domain.DataRealtime;
import org.dromara.bclt.mapper.DataRealtimeMapper;
import org.dromara.bclt.service.IDataRealtimeService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 手环-指标数据-实时测量Service业务层处理
 *
 * @author meiyoudaan448
 * @date 2024-10-21
 */
@RequiredArgsConstructor
@Service
public class DataRealtimeServiceImpl implements IDataRealtimeService {

    private final DataRealtimeMapper baseMapper;

    private final DeviceInfoMapper deviceInfoMapper;


    /**
     * 查询手环-指标数据-实时测量
     *
     * @param id 主键
     * @return 手环-指标数据-实时测量
     */
    @Override
    public DataRealtimeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询手环-指标数据-实时测量列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 手环-指标数据-实时测量分页列表
     */
    @Override
    public TableDataInfo<DataRealtimeVo> queryPageList(DataRealtimeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DataRealtime> lqw = buildQueryWrapper(bo);
        Page<DataRealtimeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的手环-指标数据-实时测量列表
     *
     * @param bo 查询条件
     * @return 手环-指标数据-实时测量列表
     */
    @Override
    public List<DataRealtimeVo> queryList(DataRealtimeBo bo) {
        LambdaQueryWrapper<DataRealtime> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DataRealtime> buildQueryWrapper(DataRealtimeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DataRealtime> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, DataRealtime::getId, bo.getId());
        lqw.eq(bo.getDeviceId() != null, DataRealtime::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getTaskId() != null, DataRealtime::getTaskId, bo.getTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getDataType()), DataRealtime::getDataType, bo.getDataType());
        lqw.eq(bo.getDataValue() != null, DataRealtime::getDataValue, bo.getDataValue());
        lqw.eq(bo.getRecordTime() != null, DataRealtime::getRecordTime, bo.getRecordTime());
        lqw.eq(bo.getCreateTime() != null, DataRealtime::getCreateTime, bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增手环-指标数据-实时测量
     *
     * @param bo 手环-指标数据-实时测量
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(DataRealtimeBo bo) {
        if(bo.getDeviceNumber() != null) {

            DeviceInfoVo deviceInfoVo = deviceInfoMapper.selectVoOne(new LambdaQueryWrapper<DeviceInfo>()
                .select(DeviceInfo::getId)
                .eq(DeviceInfo::getDeviceNumber, bo.getDeviceNumber()));
            Long deviceId = null;
            if(deviceInfoVo != null) {
                deviceId = deviceInfoVo.getId();
            }
            bo.setDeviceId(deviceId);
        }
        bo.setRecordTime(new Date());
        DataRealtime add = MapstructUtils.convert(bo, DataRealtime.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改手环-指标数据-实时测量
     *
     * @param bo 手环-指标数据-实时测量
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(DataRealtimeBo bo) {
        DataRealtime update = MapstructUtils.convert(bo, DataRealtime.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DataRealtime entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除手环-指标数据-实时测量信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
